package consul

import (
	"github.com/google/uuid"
	capi "github.com/hashicorp/consul/api"
)

type ConsulClient struct {
	client *capi.Client
}

func NewConsul(str string) *capi.Client {
	cfg := capi.DefaultConfig()
	cfg.Address = str
	client, err := capi.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	return client
}

type GrpcConf struct {
	Name    string
	Tags    []string
	Address string
	Port    int
}

func (c ConsulClient) Registeraa(cfg GrpcConf) error {
	registration := capi.AgentServiceRegistration{
		ID:      uuid.NewString(),
		Name:    cfg.Name,
		Tags:    cfg.Tags,
		Port:    cfg.Port,
		Address: cfg.Address,
	}
	return c.client.Agent().ServiceRegister(&registration)
}
